package com.icss.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.icss.model.Student;
import com.icss.model.StudentPage;
import com.icss.service.StudentService;
import com.icss.service.impl.StudentServiceImpl;

import net.sf.json.JSONObject;


@WebServlet("/StudentServlet")
public class StudentServlet extends HttpServlet {
	StudentService ss = new StudentServiceImpl();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String flag = request.getParameter("flag");
		if("findByPageName".equals(flag)) {
			findByPageName(request,response);
		}
		if("del".equals(flag)) {
			del(request,response);
		}
		if("add".equals(flag)) {
			add(request,response);
		}
		if("findById".equals(flag)) {
			findById(request,response);
		}
		if("update".equals(flag)) {
			update(request,response);
		}
	}	
	private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String sid = request.getParameter("sid");
		String user = request.getParameter("user");
		String sex = request.getParameter("sex");
		String tid = request.getParameter("tid");
		int id = 0;
		if(sid != null && !"".equals(sid)) {
			id = Integer.parseInt(sid);
		}
		int teaId = 0;
		if(tid != null && !"".equals(tid)) {
			teaId = Integer.parseInt(tid);
		}
		boolean update = ss.update(new Student(id, user, sex, teaId));
		if(update) {
			request.getRequestDispatcher("index.jsp").forward(request, response);
		}else {
			request.getRequestDispatcher("fail.jsp").forward(request, response);
		}
	}
	private void findById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String sid = request.getParameter("sid");
		int id = 0;
		if(sid != null && !"".equals(sid)) {
			id = Integer.parseInt(sid);
		}
		Student s = ss.findById(id);
		request.setAttribute("student", s);
		request.getRequestDispatcher("update.jsp").forward(request, response);
	}
	private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String user = request.getParameter("user");
		String sex = request.getParameter("sex");
		String tid = request.getParameter("tid");
		int teaId = 0;
		if(tid != null && !"".equals(tid)) {
			teaId = Integer.parseInt(tid);
		}
		boolean add = ss.add(new Student(0, user, sex, teaId));
		if(add) {
			request.getRequestDispatcher("index.jsp").forward(request, response);
		}else {
			request.getRequestDispatcher("fail.jsp").forward(request, response);
		}
	}
	private void del(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String sid = request.getParameter("sid");
		int id = 0;
		if(sid != null && !"".equals(sid)) {
			id = Integer.parseInt(sid);
		}
		response.setContentType("text/html; charset=UTF-8");
		boolean del = ss.del(id);
		if(del) {
			response.getWriter().print("成功");
		}else {
			response.getWriter().print("失败");
		}
	}
	private void findByPageName(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String name = request.getParameter("name");
		String current = request.getParameter("current");
		//当前页
		int currentPage = 1;
		if(!"".equals(current) && current != null) {
			currentPage = Integer.parseInt(current);
		}
		//当前页显示的数据
		List<Student> list = ss.findByPageName(currentPage, name);
		//总页数 = 总条数/每页显示的条数
		//总条数：模糊查询的总条数
		List<Student> listAll = ss.findByName(name);
		int total = 0;
		if(listAll.size() % StudentPage.PAGE_COUNT != 0) {
			total = listAll.size() / StudentPage.PAGE_COUNT + 1;
		}else {
			total = listAll.size() / StudentPage.PAGE_COUNT;
		}
		StudentPage sp = new StudentPage(currentPage, total, list);
		JSONObject json = JSONObject.fromObject(sp);
		response.setContentType("text/html; charset=UTF-8");
		response.getWriter().print(json);
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

}
